import VNode from "../vdom/vnode.js";
import {prepareRender} from "./render.js";
import {getTemplate2VnodeMap, getVnode2TemplateMap} from "./render.js";

export function initMount() {
    Due.prototype.$mount = function (el) {
        let vm = this;
        let rootDom = document.getElementById(el);
        mount(vm,rootDom);
    }
}

export function mount(vm, el) {
    //进行挂载
    // console.log("begin mount");
    vm._vnode = constructVNode(vm, el, null);
    //进行预备渲染(建立渲染索引，通过模板找vnode,通过vnode找模板)
    prepareRender(vm , vm._vnode);
    console.log(getTemplate2VnodeMap());
    console.log(getVnode2TemplateMap());
}

function constructVNode(vm, elm, parent) {//深度优先搜索
    let vnode = null;
    let children = [];
    let text = getNodeText(elm);//确定节点里是否有内容
    let data = null;
    let nodeType = elm.nodeType;
    let tag = elm.nodeName;
    vnode = new VNode(tag, elm, children, text, data, parent, nodeType);

    let childs = vnode.elm.childNodes;
    for(let i = 0;i < childs.length;i ++){
        let childNodes = constructVNode(vm,childs[i],vnode);
        if(childNodes instanceof VNode){
            vnode.children.push(childNodes);
        }else{
            vnode.children = vnode.children.concat(childNodes);
        }
    }
    return vnode;
}

function getNodeText(elm) {
    if(elm.nodeType == 3){
        return elm.nodeValue;
    }else{
        return "";
    }
}